//
//  BKContactPeople.m
//  bestkeep
//
//  Created by yons on 16/12/8.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKContactPeople.h"

@implementation BKContactPeople

-(instancetype)initWithDictionary:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        self.name = dict[@"name"];
        self.telephone = dict[@"telephone"];
        self.email = dict[@"email"];
        self.address = dict[@"address"];
        self.companyName = dict[@"companyName"];
    }
    return self;
}

-(void)parsePerson:(ABRecordRef)person{
    [self read:person];
}

-(void)read:(ABRecordRef)person{
    
    NSString *prefix = (__bridge NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);
    if (prefix != nil) {
        self.name = prefix;
    }else{
        self.name = @"";
    }
    NSString *lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (lastname != nil) {
        self.name = [self.name stringByAppendingString:lastname];
    }
    NSString *middlename = (__bridge NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    if (middlename != nil) {
        self.name = [self.name stringByAppendingString:middlename];
    }
    NSString *personName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    if(personName != nil)
        self.name = [self.name stringByAppendingString:personName];
    
    
    NSString *suffix = (__bridge NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);
    if (suffix != nil) {
        self.name = [self.name stringByAppendingString:suffix];
    }
    
    ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
    for (int k = 0; k<ABMultiValueGetCount(phone); k++)
    {
        NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
        self.telephone = personPhone;
        NSLog(@"%@",personPhone);
    }
    
}
- (void)getPersonAddressBookToPhoneCards:(ABRecordRef)person {
    
    NSString *prefix = (__bridge NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);
    if (prefix != nil) {
        self.name = prefix;
    }else{
        self.name = @"";
    }
    NSString *lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (lastname != nil) {
        self.name = [self.name stringByAppendingString:lastname];
    }
    NSString *middlename = (__bridge NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    if (middlename != nil) {
        self.name = [self.name stringByAppendingString:middlename];
    }
    NSString *personName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    if(personName != nil)
        self.name = [self.name stringByAppendingString:personName];
    
    
    NSString *suffix = (__bridge NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);
    if (suffix != nil) {
        self.name = [self.name stringByAppendingString:suffix];
    }
    
    ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
    
    if (ABMultiValueGetCount(phone)>0) {
        NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, 0);
        self.telephone = personPhone;
    }
}

@end
